﻿using System.Collections;
using umbraco.cms.businesslogic.member;

namespace Designit.Umb.Newsletter.Bll
{
    public class Subscribe
    {
        public static void assignMemberToGroup(Member member, string memberGroupName)
        {

            MemberGroup memberGroup = MemberGroup.GetByName(memberGroupName);
            if (!member.Groups.Contains(memberGroup.Id))
            {
                member.AddGroup(memberGroup.Id);
            }

        }

        public static void AddToActiveCampaign(Member member)
        {
            ArrayList member2Group = new ArrayList();

            //retrieve members active subscribtions
            using (var reader =
                umbraco.BusinessLogic.Application.SqlHelper.ExecuteReader("select * from cmsMember2MemberGroup where Member=@Member",
                                                                          umbraco.BusinessLogic.Application.SqlHelper.CreateParameter("@Member", member.Id)))
            {
                //store members active subscribtion id's in arraylist
                while (reader.Read())
                {
                    member2Group.Add(reader.GetInt("MemberGroup"));
                }
            }

            ArrayList campaignId = new ArrayList();

            //retrieve drafted campaigns
            using (var reader =
                umbraco.BusinessLogic.Application.SqlHelper.ExecuteReader("select id from newsLetterCampaignNewsletter where campstatus='draft'; "))
            {
                //store drafted campaign's id
                while (reader.Read())
                {
                    campaignId.Add(reader.GetInt("id"));
                }
            }

            //remove campaigns from list which member is allready subscribing to
            using (var readerMemberIds =
                umbraco.BusinessLogic.Application.SqlHelper.ExecuteReader("select campaignId from newsLetterCampaign2Member where memberId=@memberId",
                                                                          umbraco.BusinessLogic.Application.SqlHelper.CreateParameter("@memberId", member.Id)))
            {
                while (readerMemberIds.Read())
                {
                    if (campaignId.Contains(readerMemberIds.GetInt("campaignId")))
                    {
                        campaignId.Remove(readerMemberIds.GetInt("campaignId"));
                    }
                }
            }



            //add campaign subscribtion to active subscribtions
            foreach (int id in campaignId)
            {
                umbraco.BusinessLogic.Application.SqlHelper.ExecuteNonQuery(
                    "insert into newsLetterCampaign2Member(memberId, campaignId) values(@memberId, @campaignId)",
                    umbraco.BusinessLogic.Application.SqlHelper.CreateParameter("@memberId", member.Id),
                    umbraco.BusinessLogic.Application.SqlHelper.CreateParameter("@campaignId", id));
            }

        }
    }
}
